import lightning as L
from torch.utils.data import DataLoader
from omegaconf import DictConfig

from ecgcmr.signal.sig_datasets.ConrtastiveECGDataset import ContrastiveECGDataset


class ContrastiveECGDataModule(L.LightningDataModule):
    def __init__(self, cfg: DictConfig) -> None:
        super().__init__()

        self.cfg = cfg
        self.batch_size = cfg.dataset.batch_size
        self.num_workers = cfg.dataset.num_workers

    def setup(self, stage: str):
        if stage == 'fit':
            self.dataset_train = ContrastiveECGDataset(cfg=self.cfg, mode='train', apply_augmentations=True)
            self.dataset_val = ContrastiveECGDataset(cfg=self.cfg,  mode='val', apply_augmentations=False)

    def train_dataloader(self) -> DataLoader:
        return DataLoader(
            self.dataset_train, 
            batch_size=self.batch_size,
            shuffle=True, 
            num_workers=self.num_workers,
            pin_memory=True
        )

    def val_dataloader(self) -> DataLoader:
        return DataLoader(
            self.dataset_val, 
            batch_size=self.batch_size, 
            shuffle=False, 
            num_workers=self.num_workers,
            pin_memory=True
        )
